package hongcheng.code_generator.entity;

import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * 	数据库类型和对应的java类型信息
 * */
public class JdbcTypeInformation {
	/***
	 * 	数据库的字段类型
	 * */
	private String dbFieldType;
	/**
	 * 	jdbc类型
	 * */
	private int sqlType;
	/**
	 * 	对应的java类型
	 * */
	private Class<?> javaClasz;
	/**
	 * 	对应的java类完全限定名
	 * */
	private String javaClassFullName;
	/**
	 * 	对应的java类型简单类名
	 * */
	private String javaClassSimpleName;
	/**
	 * 	mybatis的jdbc类型，主要用于mybatis的xml，指明参数的jdbc类型
	 * */
	private String mybatisJdbcType;
	
	
	/**
	 * 	@param dbFieldType 数据库的字段类型 
	 * 	@param sqlType jdbc类型
	 * 	@param javaClasz 对应的java类型
	 * 	@param javaClassFullName 对应的java类完全限定名
	 * 	@param javaClassSimpleName 对应的java类型简单类名
	 * */
	public JdbcTypeInformation(String dbFieldType,int sqlType,Class<?> javaClasz,String javaClassFullName,String javaClassSimpleName) {
		this.dbFieldType = dbFieldType;
		this.sqlType = sqlType;
		this.javaClasz = javaClasz;
		this.javaClassFullName = javaClassFullName;
		this.javaClassSimpleName = javaClassSimpleName;
		this.mybatisJdbcType = MybatisJdbcType.forCode(sqlType).toString();
	}

	public String getDbFieldType() {
		return dbFieldType;
	}

	public int getSqlType() {
		return sqlType;
	}


	public Class<?> getJavaClasz() {
		return javaClasz;
	}

	public String getJavaClassFullName() {
		return javaClassFullName;
	}

	public String getJavaClassSimpleName() {
		return javaClassSimpleName;
	}


	public String getMybatisJdbcType() {
		return mybatisJdbcType;
	}


	/**
	 * 	自定义jdbc类型
	 * 	主要是根据java类型获取对应的数据库类型
	 * */
	public enum MybatisJdbcType {
		
		/*
		   * This is added to enable basic support for the
		   * ARRAY data type - but a custom type handler is still required
		   */
		  ARRAY(Types.ARRAY),
		  BIT(Types.BIT),
		  TINYINT(Types.TINYINT),
		  SMALLINT(Types.SMALLINT),
		  INTEGER(Types.INTEGER),
		  BIGINT(Types.BIGINT),
		  FLOAT(Types.FLOAT),
		  REAL(Types.REAL),
		  DOUBLE(Types.DOUBLE),
		  NUMERIC(Types.NUMERIC),
		  DECIMAL(Types.DECIMAL),
		  CHAR(Types.CHAR),
		  VARCHAR(Types.VARCHAR),
		  LONGVARCHAR(Types.LONGVARCHAR),
		  DATE(Types.DATE),
		  TIME(Types.TIME),
		  TIMESTAMP(Types.TIMESTAMP),
		  BINARY(Types.BINARY),
		  VARBINARY(Types.VARBINARY),
		  LONGVARBINARY(Types.LONGVARBINARY),
		  NULL(Types.NULL),
		  OTHER(Types.OTHER),
		  BLOB(Types.BLOB),
		  CLOB(Types.CLOB),
		  BOOLEAN(Types.BOOLEAN),
		  CURSOR(-10), // Oracle
		  UNDEFINED(Integer.MIN_VALUE + 1000),
		  NVARCHAR(Types.NVARCHAR), // JDK6
		  NCHAR(Types.NCHAR), // JDK6
		  NCLOB(Types.NCLOB), // JDK6
		  STRUCT(Types.STRUCT),
		  JAVA_OBJECT(Types.JAVA_OBJECT),
		  DISTINCT(Types.DISTINCT),
		  REF(Types.REF),
		  DATALINK(Types.DATALINK),
		  ROWID(Types.ROWID), // JDK6
		  LONGNVARCHAR(Types.LONGNVARCHAR), // JDK6
		  SQLXML(Types.SQLXML), // JDK6
		  DATETIMEOFFSET(-155); // SQL Server 2008
	
		  public final int TYPE_CODE;
		  private static Map<Integer,MybatisJdbcType> codeLookup = new HashMap<Integer,MybatisJdbcType>();
	
		  static {
		    for (MybatisJdbcType type : MybatisJdbcType.values()) {
		      codeLookup.put(type.TYPE_CODE, type);
		    }
		  }
	
		  MybatisJdbcType(int code) {
		    this.TYPE_CODE = code;
		  }
	
		  public static MybatisJdbcType forCode(int code)  {
		    return codeLookup.get(code);
		  }
	
		
	}
 
 


	
}
